<template>
  <el-main style="padding: 20px;">
    <el-button type="primary" size="mini" icon="el-icon-plus" @click="tianjia=true">
      添加分组
    </el-button>


    <div class="table-box">
      <el-table :data="list" style="width: 100%" >
        <el-table-column prop="kemu" label="章节"></el-table-column>
        <el-table-column prop="crtDate" label="日期"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="showEditView(scope.row)" type="text" size="mini">查看</el-button>
            <el-button @click="setbianji(scope.row)" type="text" size="mini">编辑</el-button>
            <el-button type="text" size="mini" @click="deleteObj(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout="prev, pager, next" :page-count="1" :current-page="dailyCur" @current-change="dailyQie"></el-pagination>
    </div>

    <el-dialog
      :visible.sync="showAddView"
      width="40%">
      <el-form class="demo-ruleForm">
           <el-form-item label="科目">
            <el-select placeholder="请选择科目" v-model="val2">
              <el-option v-for="(item,index) in zhangjieList" :key="index" :label="item.className" :value="item.classId"> {{item.className}} </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="章节名称">
              <el-input v-model="newName" placeholder="请填写章节名称"></el-input>
          </el-form-item>
      </el-form>
      <el-button type="primary" size="mini" @click="setData()">
        修改
      </el-button>
    </el-dialog>

    <el-dialog
      :visible.sync="tianjia"
      width="40%">
      <el-form class="demo-ruleForm">
          <el-form-item label="科目">
            <el-select placeholder="请选择科目" v-model="val2">
              <el-option v-for="(item,index) in zhangjieList" :key="index" :label="item.className" :value="item.classId"> {{item.className}} </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="章节名称">
              <el-input v-model="newName" placeholder="请填写章节名称"></el-input>
          </el-form-item>
      </el-form>
      <el-button type="primary" size="mini" @click="addData()">
        添加
      </el-button>
    </el-dialog>

  </el-main>
</template>

<script>
export default {
  data() {
    return {
      optionProps: {
        value: "classId",
        label: "className",
        children: "children",
      },
      dailyCur:1,
      radio:1,
      subjectId:1,
      dialogVisible:false,
      showAddView:false,
      value:'',
      val2:'',
      tianjia:false,
      course: [],
      list: [],
      testid:1,
      page: 1,
      size: 10,
      examPage:1,
      examSize:10,
      AddtEaxm:false,
      ExamList:[],
      ExamData:[],
      Examcur:1,
      selectAdd:[],
      point:'',
      typeList:[],
      dailyData:[],
      abc: [],
      zhangjieList: [],
      newName: ''
    };
  },
  inject:['reload'],
  mounted() {
    this.queryCourse().then(this.getdata);
    this.getType()
    this.getzhangjieList()
  },
  watch:{
    radio(){
      this.page = 1;
      this.getEaxm()
    }
  },
  created(){
    this.getzhangjieList()
  },
  methods: {
    handleChange() {
      this.val2 = this.abc[1];
      //console.log(this.val2);
    },
    async getzhangjieList(){
      const data = await this.postRequest('/back/class/queryPracticeClassList')
      this.zhangjieList = data.data.data
      //console.log(this.zhangjieList)
    },
    getType() {
      this.postRequest('/comm/queryCategoryByType', { type: 'practice_type' }).then(res => {
        //console.log(res);
        this.typeList = res.data.data;
        this.radio = this.typeList[0].categoryCode;
      });
    },
     handleClose(done) {
            this.$confirm('确认关闭？')
              .then(_ => {
                done();
              })
              .catch(_ => {});
          },
    okExam(){
      if(this.point==''){
        this.point = 1;
      }
      let list =[];
      let that =this;
      this.selectAdd.forEach(item=>{
        let obj = {
          practiceId:item.practiceId,
          point:that.point
        }
        list.push(obj)
      })
      //console.log(list)
      let listNew = [];
      this.postRequest(`/back/practicetest/getPracticeTest?testId=${this.examId}`).then(res => {
        listNew = res.data.data.content;
        let arr = [];
        for(var item in listNew){
          arr = arr.concat(listNew[item])
        }
        //console.log(arr)
        arr.forEach(item=>{
          list.push({practiceId:item.practiceId,point:item.point})
        })
        that.postJson(`/back/practicetest/addTestContent?testId=${that.examId}`, list).then(res => {
          //console.log(res);
          that.reload();
          that.$message(res.data.msg);
        });
      });
      //console.log(list)
    },
    tanchuExam(){
      //console.log(this.selectAdd)
      this.dialogVisible = true;
    },
    dailyQie(val) {
      //console.log(val);
      this.page = val;
      this.getdata();
    },
    ExamQie(val) {
      //console.log(val);
      this.examPage = val;
      this.getEaxm();
    },
    getEaxm(){
      let params = {
        page: this.examPage,
        size: this.examSize,
        subjectId: this.subjectId,
        type:this.radio
      };
      this.postRequest(`/back/practicestores/queryPracticeList`, params).then(res => {
        this.ExamList = res.data.data.list;
        this.ExamData = res.data.data;
        //console.log(res);
      });
    },
    selectEaxm(e){
      //console.log(e)
      this.selectAdd = e;
    },
    addWord(e){
      //console.log(e)
      this.subjectId = e.subjectId;
      this.getEaxm()
      this.examId = e.testId,
      this.AddtEaxm = true;
    },
    handleSelectionChange(val) {
      //console.log(val)
      this.multipleSelection = val;
    },
    setbianji(data){
      //console.log(data)
      this.testid = data.chapterId;
      this.showAddView=!this.showAddView;
    },
    // 删除,批量删除已逗号隔开
    deleteObj(e){
      //console.log(e)
      this.postRequest(`/back/chapter/delete?ids=${e.chapterId}`).then(res => {
        //console.log(res);
        this.$message(res.data.msg);
        this.reload()
      });
    },
    addData(){//添加没日一练
      let params = {classId:this.val2,chapterName:this.newName};
      //console.log(params)
      this.postJson('/back/chapter/addPracticeChapter',{classId:this.val2,chapterName:this.newName}).then(res => {
        //console.log(res)
        this.$message(res.data.msg);
        this.reload()
      });
    },
    showEditView(shu){    // 获取每日一练详情
      this.$router.push({path:'/shop/unit?id='+shu.chapterId});
    },
    setData(){
        let params = {classId:this.val2,chapterName:this.newName};
        //console.log(params,this.testid)
        this.postJson(`/back/chapter/updatePracticeChapter`,{classId:this.val2,chapterName:this.newName,chapterId:this.testid}).then(res => {
           this.$message(res.data.msg);
           this.reload()
        });
    },
    queryCourse() {
      let that = this;
      return new Promise(function(next, err) {
        that.postRequest('/back/chapter/queryPracticeChapterList').then(res => {
          //console.log(res);
          that.course = res.data.data;
          next()
        });
      });
    },
    getdata() {
      let that = this;
      let params = { page: this.page, size: this.size };
      return new Promise(function(next, err) {
        // that.postRequest('/back/practicetest/queryPracticeTestList', params).then(res => {
        that.postRequest('/back/chapter/queryPracticeChapterList', params).then(res => {
          //console.log(res)
          that.dailyData = res.data.data;
          that.list = res.data.data;
          that.list.forEach((item)=>{
            that.course.forEach(value=>{
              if(item.chapterId == value.chapterId){
                item.kemu=value.chapterName;
              }
            })
          })
          next()
        });
      });
    }
  }
};
</script>

<style>
/* POST /back/circleinfos/query */
</style>
<!-- postRequest -->
